package com.yuanchu.media.controller;

import com.yuanchu.common.utils.R;
import com.yuanchu.common.validator.ValidationGroups;
import com.yuanchu.media.dto.EditFileParamsDto;
import com.yuanchu.media.dto.FileIdsDto;
import com.yuanchu.media.dto.FileQueryParams;
import com.yuanchu.media.dto.PageResult;
import com.yuanchu.media.entity.YoshopUploadFileEntity;
import com.yuanchu.media.service.YoshopUploadFileService;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @version: 1.0
 * @Author: adan
 * @description:
 * @date: 2024-01-26 10:54
 */
@RestController
public class FileController {

    @Resource
    private YoshopUploadFileService uploadFileService;

    @GetMapping("/list")
    public R queryList(FileQueryParams params) {
        PageResult list = uploadFileService.queryFiles(params);
        Map<String, Object> map = new HashMap<>();
        map.put("list", list);
        return R.ok().put("data", map);
    }

    @PostMapping("/edit")
    public R edit(@RequestBody @Validated({ValidationGroups.Update.class}) EditFileParamsDto params) {
        boolean flag = uploadFileService.editFile(params);
        if (!flag) {
            return R.error("修改失败");
        }
        return R.ok("修改成功");
    }

    @PostMapping("/delete")
    public R deleteFiles(@RequestBody @Validated({ValidationGroups.Insert.class}) FileIdsDto params) {
        boolean flag = uploadFileService.deleteFiles(params);
        if (!flag) {
            return R.error("文件关联商品信息，无法删除");
        }
        return R.ok("删除成功");
    }

    @ApiOperation(value = "根据id查询文件信息")
    @GetMapping("/file/{fileId}")
    public YoshopUploadFileEntity getUploadFileById(@PathVariable("fileId") Integer imageId) {
        return uploadFileService.getUploadFileById(imageId);
    }

    @ApiOperation(value = "根据id查询文件信息")
    @GetMapping("/fileList")
    public List<YoshopUploadFileEntity> getUploadFileByIds(@RequestParam("fileIds") List<Integer> fileIds) {
        return uploadFileService.getUploadFileByIds(fileIds);
    }

}
